/* GSManufactureWriteAction.java
 *
 * Project/Product: APEP-JZCG
 *
 * Modify Information:
 * =============================================================================
 *   Author	Date       Description
 *   ----------	---------- ---------------------------------------------------
 *   peng.ning	2008-10-28	First release
 *
 * Licensed Materials - Property of APEP
 * =============================================================================
 *    (C) COPYRIGHT Shanghai Auto Mart E-Procurement Co., Ltd. 2007 - 2008
 *    All Rights Reserved.
 *
 *    This software is the confidential and proprietary information of
 *    Shanghai Auto Mart E-Procurement Co., Ltd. ("Confidential Information").
 *    You shall not disclose such Confidential Information and shall use it only
 *    in accordance with the terms of the license agreement you entered into 
 *    with Shanghai Auto Mart E-Procurement Co., Ltd.
 *
 * Noties:
 * =============================================================================
 * 
 */

package com.hitrust.gs.action;

import java.util.ArrayList;

import system.LoginContext;
import system.base.User;

import com.apep.gw.service.StuationAnalyse;
import com.apep.gw.service.impl.ProductStuationAnalyse;
import com.hitrust.gs.bean.GSManufactureBean;
import com.hitrust.gs.bean.GSProductBidBean;
import com.hitrust.gs.bean.GSProductTypeBean;
import com.hitrust.gs.business.GSManufactureProcessor;
import com.hitrust.gs.dao.GSProductBidDao;
import com.hitrust.gs.dao.GSProductTypeDao;
import com.hitrust.gs.form.GSManufactureAddForm3;
import com.hitrust.gs.lib.GSBaseConstants;
import com.hitrust.gs.lib.GSLogWriter;
import com.hitrust.gs.lib.GSSystemConfigurator;
import com.hitrust.gs.service.GSBaseAction;
import com.hitrust.gs.service.GSBaseForm;
import com.hitrust.gs.util.exception.GSException;
import com.hitrust.gs.util.page.ResultProcess;
import com.hitrust.gs.vo.GSManufactureVO;
import com.hitrust.gs.vo.GSManufactureView;

import framework.core.ConstDefine;
import framework.core.DBConnection;
import framework.core.DateUtil;

public class GSManufactureWriteAction extends GSBaseAction {

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.apep.jzcg.service.GSBaseAction#execute(com.apep.jzcg.service.GSBaseForm
	 * )
	 */
	public String execute(GSBaseForm aForm) throws Exception {
		boolean isTel=false;
    	LoginContext loginInfo =(LoginContext)aForm.getRequest().getSession().getAttribute("LoginContext");
    	User user=loginInfo.getUser();
    	if(user!=null&&user.getTel()!=null&&user.getTel().equals("1")){
    		isTel=true;
        }
		GSManufactureProcessor processor = new GSManufactureProcessor();
		GSManufactureAddForm3 form = (GSManufactureAddForm3) aForm;
		LoginContext context = (LoginContext) form.getSession().getAttribute(
				GSBaseConstants.LOGIN);
		String[] index = form.getSel_manufacture();
		String[] rid = form.getGs_recorder_id();
		ResultProcess rst = (ResultProcess) aForm.getSession().getAttribute(
				"manufactureExcel");
		ArrayList array = rst.getAllRecoreds();
		boolean flag = false;
		StuationAnalyse productStuationAnalyse = new ProductStuationAnalyse();
		Object modify = aForm.getSession().getAttribute("modify");
		if (modify != null && modify.toString().equals("true")) {
			flag = true;
		}
		try {
			if (GSBaseConstants.PLANPRICE.equals("true")) {
				for (int a = 0; a < index.length; a++) {
					int i = 0;
					for (int tJ = 0; tJ < rid.length; tJ++) {
						if (index[a].equals(rid[tJ])) {
							i = tJ;
							break;
						}
					}

					String category = form.getGs_manufacture_category_id()[i];
					String concentricityid = form.getGs_concentricity_id()[i];

					String manufactureCode = "";
					if (flag) {
						GSManufactureBean bean = processor
								.getManufactureDetailByConcentricityCode(concentricityid);
						manufactureCode = bean.getGs_manufacture_id();
					} else {
						manufactureCode = processor.getManufactureId(a);
					}
					String manufactureName = form.getGs_manufacture_name()[i];
					String providerid = form.getGs_provider_id()[i];
					String unitprice = form.getGs_manufacture_unitprice()[i];
					String planprice = form.getGs_manufacture_planprice()[i];
					String quantunit = form.getGs_quant_unit()[i];
					String manufacturetype = form.getGs_manufacture_type()[i];
					String manufacturespec = form.getGs_manufacture_spec()[i];
					String qualityassureperiod = form
							.getGs_quality_assure_period()[i];
					String manufacturername = form.getGs_manufacturer_name()[i];
					String makearea = form.getGs_make_area()[i];
					String manufacturebrand = form.getGs_manufacture_brand()[i];
					String drawingpiececode = form.getGs_drawing_piece_code()[i];
					String isbuy = form.getIs_buy()[i];
					String machinetype = form.getGs_adapt_machine_type()[i];
					String property = form.getGs_appender_property()[i];
					String remark = form.getGs_remark()[i];
					String kmQualityassureperiod = form
							.getKm_quality_assure_period()[i];
					String balancecustomercode=form.getBalance_customer_code()[i];
					// String mode = form.getGs_balance_mode()[i];
					GSManufactureVO vo = new GSManufactureVO();
					vo.setGs_manufacture_id(manufactureCode);
					vo.setGs_concentricity_id(concentricityid);
					vo.setGs_manufacture_name(manufactureName);
					vo.setGs_manufacture_category_id(category);
					vo.setGs_manufacture_brand(manufacturebrand);
					vo.setGs_manufacture_type(manufacturetype);
					vo.setGs_manufacture_spec(manufacturespec);
					vo.setGs_manufacture_unitprice(unitprice);
					vo.setGs_quant_unit(quantunit);
					vo.setGs_quality_assure_period(qualityassureperiod);
					vo.setGs_manufacturer_name(manufacturername);
					vo.setGs_make_area(makearea);
					vo.setGs_provider_id(providerid);
					vo.setGs_adapt_machine_type(machinetype);
					vo.setGs_recorder_id(context.getUser().getCode());
					vo.setGs_manufacture_record_date(new DateUtil()
							.getFullString());
					vo.setGs_is_tradeflat_manufacture("1");
					vo.setGs_appender_property(property);
					vo.setGs_remark(remark);
					vo.setGsValidity("1");
					vo.setGs_drawing_peice_code(drawingpiececode);
					vo.setIs_buy(isbuy);
					vo.setGs_balance_mode("2");
					vo.setIsupdate(GSBaseConstants.IS_UPDATE);
					vo.setIs_normal(GSBaseConstants.IS_NORMAL);
					vo.setCreate_user_id(context.getLoginUserId());
					vo.setCreate_user(context.getLoginUserName());
					vo.setCreate_time(new DateUtil().getFullString());
					vo.setLast_update_time(new DateUtil().getFullString());
					vo.setLast_update_user_id(context.getLoginUserId());
					vo.setLast_update_user(context.getLoginUserName());
					vo.setBalance_customer_code("0");
					vo.setPic(" ");
					vo.setRecent_adjustprice_date(new DateUtil()
							.getFullString());
					vo.setRecent_adjusted_price(unitprice);
					vo.setGs_planprice(planprice);
					vo.setWarehouse_code(ConstDefine.T_WARE_HOUSE_CODE);
					vo.setWarehouse_name(ConstDefine.T_WAREE_HOUSE_NAME);
					vo.setKm_quality_assure_period(kmQualityassureperiod);
					vo.setBalance_customer_code(balancecustomercode);
					String productType = form.getProduct_type()[i];
					String productBid = form.getProduct_bid()[i];
					// 判断是否存在相同集中编码的材料信息
					if (concentricityid.trim().length() != GSBaseConstants.PRODUCT_CONCENTRICITY
							&& GSBaseConstants.PRODUCT_IS_CONCENTRICITY == 0) {
						throw new GSException(
								"已成功写入: "
										+ a
										+ " 条产品信息;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;序号："
										+ form.getGs_manufacture_id()[i]
										+ "的集中编码 "
										+ vo.gs_concentricity_id
										+ " 长度必须为"
										+ GSBaseConstants.PRODUCT_CONCENTRICITY
										+ "位，<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;请修改或移除！");
					} else if (concentricityid.trim().length() != GSBaseConstants.TEMPORARY_PRODUCT_CONCENTRICITY
							&& GSBaseConstants.PRODUCT_IS_CONCENTRICITY == 0) {
						throw new GSException(
								"已成功写入: "
										+ a
										+ " 条产品信息;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;序号："
										+ form.getGs_manufacture_id()[i]
										+ "的集中编码 "
										+ vo.gs_concentricity_id
										+ " 长度必须为"
										+ GSBaseConstants.TEMPORARY_PRODUCT_CONCENTRICITY
										+ "位，<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;请修改或移除！");

					}
					GSManufactureBean mbean = processor
							.getManufInfoByConcentricityId(concentricityid);
					GSProductTypeDao dao = new GSProductTypeDao();
					GSProductBidDao bidDao = new GSProductBidDao();
					if (flag) {
						if (mbean == null) {
							throw new GSException(
									"已成功写入: "
											+ a
											+ " 条产品信息;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;序号："
											+ form.getGs_manufacture_id()[i]
											+ "的集中编码 "
											+ vo.gs_concentricity_id
											+ " 不存在，<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;不能更新修改，请修改或移除！");
						}
					} else {
						if (mbean != null) {
							throw new GSException(
									"已成功写入: "
											+ a
											+ " 条产品信息;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;序号："
											+ form.getGs_manufacture_id()[i]
											+ "的集中编码 "
											+ vo.gs_concentricity_id
											+ " 已存在，<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;不能写入，请修改或移除！");
						}
					}
					boolean bl = false;
					if (flag) {
						bl = processor.modifyManufactureKM(vo);
						if (vo.getIs_buy().equals("0")) {
							// 设置采购属性为0时 修改对应的产品使用分类和产品招标分类
							GSProductTypeBean bean = dao
									.findProductTypeByProuductCode(vo
											.getGs_concentricity_id().trim());
							if (bean != null) {
								dao.updatePrudctTypeStatusByProductCode(vo
										.getGs_concentricity_id().trim());
							}
							GSProductBidBean bidBean = bidDao
									.findProductBidByProuductCode(vo
											.getGs_concentricity_id().trim());
							if (bidBean != null) {
								bidDao.updatePrudctBidStatusByProductCode(vo
										.getGs_concentricity_id());
							}
						} else if (vo.getIs_buy().equals("1")) {

							// 设置采购属性为1时 修改对应的产品使用分类和产品招标分类
							GSProductTypeBean bean = dao
									.findProductTypeByProuductCode(vo
											.getGs_concentricity_id().trim());
							if (bean != null) {
								dao.updatePrudctTypeIsStatusByProductCode(vo
										.getGs_concentricity_id().trim());
							} else {
								bean = new GSProductTypeBean();
								bean.setCreateTime(new DateUtil()
										.getFullString());
								bean.setCreateUser(context.getLoginUserName());
								bean.setCreateUserId(context.getLoginUserId());
								bean.setIsBalance(1);
								bean.setIsBid(productBid.equals("") ? GSBaseConstants.PRODUCT_TYPE_MANAGER_ORTHER_BID
										: Integer.parseInt(productBid));
								bean.setLastUpdateTime(new DateUtil()
										.getFullString());
								bean.setLastUpdateUser(context
										.getLoginUserName());
								bean.setLastUpdateUserId(context
										.getLoginUserId());
								bean.setProductCode(vo.getGs_concentricity_id()
										.trim());
								bean.setProductType(productType.equals("") ? GSBaseConstants.PRODUCT_TYPE_MANAGER_ORTHER_USE
										: Integer.parseInt(productType));
								bean.setProviderCode(mbean.getGs_provider_id());
								bean.setProductId(mbean.getId());
								bean.setRemark("");
								bean.setStatus(Integer
										.parseInt(ConstDefine.STR_VALID_YES));
								dao.insert(bean);
							}
							GSProductBidBean bidBean = bidDao
									.findProductBidByProuductCode(vo
											.getGs_concentricity_id().trim());
							if (bidBean != null) {
								bidDao.updatePrudctIsBidStatusAndTypeAndIsBidByProductCode(vo
										.getGs_concentricity_id());
							} else {
								bidBean = new GSProductBidBean();
								bidBean.setCreateTime(new DateUtil()
										.getFullString());
								bidBean.setCreateUser(context
										.getLoginUserName());
								bidBean.setCreateUserId(context
										.getLoginUserId());
								bidBean.setProductName(mbean
										.getGs_manufacture_name());
								bidBean.setLastUpdateTime(new DateUtil()
										.getFullString());
								bidBean.setLastUpdateUser(context
										.getLoginUserName());
								bidBean.setLastUpdateUserId(context
										.getLoginUserId());
								bidBean.setProductCode(vo
										.getGs_concentricity_id().trim());
								bidBean.setProductType(productBid.equals("") ? GSBaseConstants.PRODUCT_TYPE_MANAGER_ORTHER_BID
										: Integer.parseInt(productBid));
								bidBean.setProviderCode(mbean
										.getGs_provider_id().trim());
								bidBean.setProductId(mbean.getId());
								bidBean.setRemark("");
								bidBean.setStatus(Integer
										.parseInt(ConstDefine.STR_VALID_YES));
								bidDao.insert(bidBean);
							}
						}
					} else {

						if (!dao.isProductTypeByType(productType.trim())) {
							throw new GSException(
									"已成功写入: "
											+ a
											+ " 条产品信息;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;序号："
											+ form.getGs_manufacture_id()[i]
											+ "的集中编码 ，产品使用分类："
											+ productType
											+ " 不存在，<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;不能更新修改，请修改或移除！");
						}
						if (!productBid.trim().equals("")
								&& !bidDao
										.isProductBidByType(productBid.trim())) {
							throw new GSException(
									"已成功写入: "
											+ a
											+ " 条产品信息;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;序号："
											+ form.getGs_manufacture_id()[i]
											+ "的集中编码，产品招标分类： "
											+ productBid
											+ " 不存在，<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;不能更新修改，请修改或移除！");
						}
						bl = processor.saveManufacture(vo);
						GSProductTypeBean bean = dao
								.findProductTypeByProuductCode(vo
										.getGs_concentricity_id().trim());
						if (bean != null) {
							bean.setProductCode(vo.getGs_concentricity_id()
									.trim());
							bean.setProviderCode(vo.getGs_provider_id());
							bean.setProductType(productType.equals("") ? GSBaseConstants.PRODUCT_TYPE_MANAGER_ORTHER_USE
									: Integer.parseInt(productType));
							bean.setIsBid(productBid.equals("") ? GSBaseConstants.PRODUCT_TYPE_MANAGER_ORTHER_BID
									: Integer.parseInt(productBid));
							bean.setLastUpdateTime(new DateUtil()
									.getFullString());
							bean.setLastUpdateUser(context.getLoginUserName());
							bean.setLastUpdateUserId(context.getLoginUserId());
							dao.update(bean);
						} else {
							bean = new GSProductTypeBean();
							bean.setCreateTime(new DateUtil().getFullString());
							bean.setCreateUser(context.getLoginUserName());
							bean.setCreateUserId(context.getLoginUserId());
							bean.setIsBalance(1);
							bean.setIsBid(productBid.equals("") ? GSBaseConstants.PRODUCT_TYPE_MANAGER_ORTHER_BID
									: Integer.parseInt(productBid));
							bean.setLastUpdateTime(new DateUtil()
									.getFullString());
							bean.setLastUpdateUser(context.getLoginUserName());
							bean.setLastUpdateUserId(context.getLoginUserId());
							bean.setProductCode(vo.getGs_concentricity_id()
									.trim());
							bean.setProductType(productType.equals("") ? GSBaseConstants.PRODUCT_TYPE_MANAGER_ORTHER_USE
									: Integer.parseInt(productType));
							bean.setProviderCode(vo.getGs_provider_id());
							bean.setProductId(mbean.getId());
							bean.setRemark("");
							bean.setStatus(Integer
									.parseInt(ConstDefine.STR_VALID_YES));
							dao.insert(bean);
						}
						GSProductBidBean bidBean = bidDao
								.findProductBidByProuductCode(vo
										.getGs_concentricity_id().trim());
						if (bidBean != null) {
							bidBean.setProductId(mbean.getId());
							bidBean.setProductCode(vo.getGs_concentricity_id()
									.trim());
							bidBean.setProviderCode(vo.getGs_provider_id());
							bidBean.setProductType(Integer
									.parseInt(productType));
							bidBean.setLastUpdateTime(new DateUtil()
									.getFullString());
							bidBean.setLastUpdateUser(context
									.getLoginUserName());
							bidBean.setLastUpdateUserId(context
									.getLoginUserId());
							bidDao.update(bidBean);
						} else {
							bidBean = new GSProductBidBean();
							bidBean.setCreateTime(new DateUtil()
									.getFullString());
							bidBean.setCreateUser(context.getLoginUserName());
							bidBean.setCreateUserId(context.getLoginUserId());
							bidBean.setProductName(vo.getGs_manufacture_name());
							bidBean.setLastUpdateTime(new DateUtil()
									.getFullString());
							bidBean.setLastUpdateUser(context
									.getLoginUserName());
							bidBean.setLastUpdateUserId(context
									.getLoginUserId());
							bidBean.setProductCode(vo.getGs_concentricity_id()
									.trim());
							bidBean.setProductType(productBid.equals("") ? GSBaseConstants.PRODUCT_TYPE_MANAGER_ORTHER_BID
									: Integer.parseInt(productBid));
							bidBean.setProviderCode(vo.getGs_provider_id()
									.trim());
							bidBean.setProductId(vo.getId());
							bidBean.setRemark("");
							bidBean.setStatus(Integer
									.parseInt(ConstDefine.STR_VALID_YES));
							bidDao.insert(bidBean);
						}
					}
					if (bl) {
						// 移除
						int row = array.size();
						for (int j = 0; j < row; j++) {
							GSManufactureView mv = (GSManufactureView) array
									.get(j);
							if (index[a].equals(mv.getGs_recorder_id())) {
								array.remove(mv);
								break;
							}
						}
					}

				}
				if (array != null && array.size() == 0) {
					aForm.getSession().removeAttribute("manufactureExcel");
					aForm.getRequest().setAttribute("message",
							"产品确认写入成功，无可确认产品信息，返回继续添加。");
					aForm.getRequest().setAttribute("action", "A400313.do");
				} else if (array != null && array.size() > 0) {
					aForm.getRequest().setAttribute("message", "产品确认写入成功");
					if (aForm.getRequest().getParameter("choice").equals("")) {
						aForm.getRequest().setAttribute("action", "A400312.do");
					}
				}
			} else {
				for (int a = 0; a < index.length; a++) {
					int i = 0;
					for (int tJ = 0; tJ < rid.length; tJ++) {
						if (index[a].equals(rid[tJ])) {
							i = tJ;
							break;
						}
					}

					String category = form.getGs_manufacture_category_id()[i];
					String concentricityid = form.getGs_concentricity_id()[i];
					String manufactureCode = "";
					if (flag) {
						GSManufactureBean bean = processor
								.getManufactureDetailByConcentricityCode(concentricityid);
						manufactureCode = bean.getGs_manufacture_id();
					} else {
						manufactureCode = processor.getManufactureId(a);
					}

					String manufactureName = form.getGs_manufacture_name()[i];
					String providerid = form.getGs_provider_id()[i];
					String unitprice = form.getGs_manufacture_unitprice()[i];
					String planprice = "";
					if(flag==false&&isTel){
						 planprice=form.getGs_manufacture_planprice()[i];
					}
					String quantunit = form.getGs_quant_unit()[i];
					String manufacturetype = form.getGs_manufacture_type()[i];
					String manufacturespec = form.getGs_manufacture_spec()[i];
					String qualityassureperiod = form
							.getGs_quality_assure_period()[i];
					String manufacturername = form.getGs_manufacturer_name()[i];
					String makearea = form.getGs_make_area()[i];
					String manufacturebrand = form.getGs_manufacture_brand()[i];
					String drawingpiececode = form.getGs_drawing_piece_code()[i];
					String isbuy = form.getIs_buy()[i];
					String machinetype = form.getGs_adapt_machine_type()[i];
					String property = form.getGs_appender_property()[i];
					String remark = form.getGs_remark()[i];
					String mode = form.getGs_balance_mode()[i];
					String kmQualityassureperiod = form
							.getKm_quality_assure_period()[i];
					String balancecustomercode=form.getBalance_customer_code()[i];
					
					GSManufactureVO vo = new GSManufactureVO();
					vo.setGs_manufacture_id(manufactureCode);
					vo.setGs_concentricity_id(concentricityid);
					vo.setGs_manufacture_name(manufactureName);
					vo.setGs_manufacture_category_id(category);
					vo.setGs_manufacture_brand(manufacturebrand);
					vo.setGs_manufacture_type(manufacturetype);
					vo.setGs_manufacture_spec(manufacturespec);
					vo.setGs_manufacture_unitprice(unitprice);
					vo.setGs_quant_unit(quantunit);
					vo.setGs_quality_assure_period(qualityassureperiod);
					vo.setGs_manufacturer_name(manufacturername);
					vo.setGs_make_area(makearea);
					vo.setGs_provider_id(providerid);
					vo.setGs_adapt_machine_type(machinetype);
					vo.setGs_recorder_id(context.getUser().getCode());
					vo.setGs_manufacture_record_date(new DateUtil()
							.getFullString());
					vo.setGs_is_tradeflat_manufacture("1");
					vo.setGs_appender_property(property);
					vo.setGs_remark(remark);
					vo.setGsValidity("1");
					vo.setGs_drawing_peice_code(drawingpiececode);
					vo.setIs_buy(isbuy);
					vo.setGs_balance_mode(mode);
					vo.setIsupdate(GSBaseConstants.IS_UPDATE);
					vo.setKm_quality_assure_period(kmQualityassureperiod);
					String productType = form.getProduct_type()[i];
					String productBid = form.getProduct_bid()[i];
					vo.setProductType(productType);
					vo.setBidType(productBid);
					if (flag) {
						if (Integer.parseInt(GSSystemConfigurator
								.getParam("gs.is.concentricity")) == 0) {
							if (concentricityid.trim().length() == GSBaseConstants.TEMPORARY_PRODUCT_CONCENTRICITY) {
								vo.setIs_normal(GSBaseConstants.NOT_NORMAL);
							} else
								vo.setIs_normal(GSBaseConstants.IS_NORMAL);
						} else
							vo.setIs_normal(GSBaseConstants.IS_NORMAL);
					} else
						vo.setIs_normal(GSBaseConstants.IS_NORMAL);

					vo.setCreate_user_id(context.getLoginUserId());
					vo.setCreate_user(context.getLoginUserName());
					vo.setCreate_time(new DateUtil().getFullString());
					vo.setLast_update_time(new DateUtil().getFullString());
					vo.setLast_update_user_id(context.getLoginUserId());
					vo.setLast_update_user(context.getLoginUserName());
					vo.setBalance_customer_code(balancecustomercode);
					vo.setPic(" ");
					vo.setRecent_adjustprice_date(new DateUtil()
							.getFullString());
					vo.setRecent_adjusted_price(unitprice);
					vo.setGs_planprice(planprice);
					GSProductTypeDao dao = new GSProductTypeDao();
					GSProductBidDao bidDao = new GSProductBidDao();
					if (flag) {
						vo.setWarehouse_code("");
						vo.setWarehouse_name("");
					} else {
						vo.setWarehouse_code(ConstDefine.T_WARE_HOUSE_CODE);
						vo.setWarehouse_name(ConstDefine.T_WAREE_HOUSE_NAME);
					}

					if (GSBaseConstants.PLANPRICE.equals("false")&&flag==false) {
						vo.setGs_planprice(unitprice);
					}
					if(isTel){
						vo.setGs_planprice(planprice);
					}else{
						if(flag==false){
							vo.setGs_planprice(unitprice);
							planprice=unitprice;
						}
						
					}

//					if (GSBaseConstants.IS_CENTER_WAREHOUSE!=null&&GSBaseConstants.IS_CENTER_WAREHOUSE.equals("true")) {
//						//planprice=form.getGs_manufacture_planprice()[i];
//						planprice=unitprice;
//					}
					// 判断是否存在相同集中编码的材料信息
					if (Integer.parseInt(GSSystemConfigurator
							.getParam("gs.is.concentricity")) == 0) {
						if (concentricityid.trim().length() != GSBaseConstants.PRODUCT_CONCENTRICITY) {
							if (!flag) {
								//ZT改
								String endStr = concentricityid.trim().substring(concentricityid.trim().length()-1);
								if(concentricityid.trim().length() == GSBaseConstants.PRODUCT_CONCENTRICITY2&&endStr.equals("0")){
									
								}else{
									throw new GSException(
										"已成功写入: "
												+ a
												+ " 条产品信息;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;序号："
												+ form.getGs_manufacture_id()[i]
												+ "的集中编码 "
												+ vo.gs_concentricity_id
												+ " 长度必须为"
												+ GSBaseConstants.PRODUCT_CONCENTRICITY
												+ "位或"+GSBaseConstants.PRODUCT_CONCENTRICITY2+"位末尾为0，<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;请修改或移除！");
								}

							} else {
								if (concentricityid.trim().length() != GSBaseConstants.TEMPORARY_PRODUCT_CONCENTRICITY) {
									throw new GSException(
											"已成功写入: "
													+ a
													+ " 条产品信息;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;序号："
													+ form.getGs_manufacture_id()[i]
													+ "的集中编码 "
													+ vo.gs_concentricity_id
													+ " 长度必须为"
													+ GSBaseConstants.TEMPORARY_PRODUCT_CONCENTRICITY
													+ "位或者"
													+ GSBaseConstants.PRODUCT_CONCENTRICITY
													+ "位，<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;请修改或移除！");
								}
							}

						}

					} else {

					}

					GSManufactureBean mbean = processor
							.getManufInfoByConcentricityId(concentricityid);
					if (flag) {
						if (mbean == null) {
							throw new GSException(
									"已成功写入: "
											+ a
											+ " 条产品信息;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;序号："
											+ form.getGs_manufacture_id()[i]
											+ "的集中编码 "
											+ vo.gs_concentricity_id
											+ " 不存在，<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;不能更新修改，请修改或移除！");
						} else if (mbean.getIs_normal().equals("0")
								&& mbean.getGs_concentricity().length() != 14) {
							throw new GSException(
									"已成功写入: "
											+ a
											+ " 条产品信息;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;序号："
											+ 1
											+ "的集中编码 "
											+ vo.gs_concentricity_id
											+ " 是已存在的临时产品，<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;不能写入，请修改或移除！");
						} else if (!mbean.getGs_provider_id().equals(
								vo.getGs_provider_id())
								&& !vo.getGs_provider_id().equals("")) {
							throw new GSException(
									"已成功写入: "
											+ a
											+ " 条产品信息;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;序号："
											+ 1
											+ "的集中编码 "
											+ vo.gs_concentricity_id
											+ " 的供应商错误，<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;不能写入，请修改或移除！");
						}

					} else {
						if (mbean != null) {
							throw new GSException(
									"已成功写入: "
											+ a
											+ " 条产品信息;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;序号："
											+ form.getGs_manufacture_id()[i]
											+ "的集中编码 "
											+ vo.gs_concentricity_id
											+ " 已存在，<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;不能写入，请修改或移除！");
						}
					}

					boolean bl = false;

					if (flag) {
						//修改保存时,是否可采购，产品单价不改
						vo.setIs_buy(mbean.getIs_buy());
						vo.setGs_manufacture_unitprice(mbean.getGs_manufacture_unitprice()+"");
						//修改进价,用产品单价改计划价
						if(isTel){
							if(unitprice!=null&&unitprice.trim().length()>0){
								double planPrice1=Double.parseDouble(unitprice);
								if(mbean.getPlan_price()!=planPrice1){
									vo.setGs_planprice(unitprice);
								}
								
							}
						}
	
						
						
						String km = mbean.getKm_quality_assure_period();
						if (vo.getKm_quality_assure_period().equals("")) {
							vo.setKm_quality_assure_period(km);
						}
					
						bl = processor.modifyManufactureByConditionKM(vo);
				
						if(mbean.getIs_buy().equals("0")){
							productStuationAnalyse.open(processor.appendUpdateIsBuy(mbean, vo), vo.getLast_update_time(), context.getLoginUserId(),context.getLoginUserName(),mbean!=null?mbean.getId():"");
						}
						if(vo.getGs_manufacture_unitprice()!=null&&!vo.getGs_manufacture_unitprice().equals("")&&Double.parseDouble(vo.getGs_manufacture_unitprice())!=mbean.getGs_manufacture_unitprice()){
							productStuationAnalyse.changePrice(processor.appendUpdatePrice(mbean, vo), vo.getLast_update_time(),context.getLoginUserId(),context.getLoginUserName(),mbean!=null?mbean.getId():"");
						}
						
						if(!"".equals(processor.appendUpdateContent(mbean, vo))){
							productStuationAnalyse.update(processor.appendUpdateContent(mbean, vo),
								vo.getCreate_time(), context.getLoginUserId(),
								context.getLoginUserName(), mbean.getId());
						}
						
						if (vo.getIs_buy().equals("0")) {
							productStuationAnalyse.close(
									processor.appendUpdateIsBuy(mbean, vo),
									vo.getLast_update_time(),
									context.getLoginUserId(),
									context.getLoginUserName(), mbean.getId());

							// 设置采购属性为0时 修改对应的产品使用分类和产品招标分类
							GSProductTypeBean bean = dao
									.findProductTypeByProuductCodeNoStauts(vo
											.getGs_concentricity_id().trim());
							if (bean != null) {
								dao.updatePrudctTypeStatusByProductCode(vo
										.getGs_concentricity_id().trim());
							}
							GSProductBidBean bidBean = bidDao
									.findProductBidByProuductCodeNoStauts(vo
											.getGs_concentricity_id().trim());
							if (bidBean != null) {
								bidDao.updatePrudctBidStatusByProductCode(vo
										.getGs_concentricity_id());
							}
						} else {
							// 设置采购属性为1时 修改对应的产品使用分类和产品招标分类
							String providerCode = vo.getGs_provider_id().trim()
									.equals("") ? mbean.getGs_provider_id()
									: vo.getGs_provider_id().trim();
							String productName = vo.getGs_manufacture_name()
									.trim().equals("") ? mbean
									.getGs_manufacture_name() : vo
									.getGs_manufacture_name().trim();

								GSProductTypeBean bean = dao
										.findProductTypeByProuductCodeNoStauts(vo
												.getGs_concentricity_id()
												.trim());
								if (bean != null) {
									bean.setStatus(1);
									bean.setProductCode(vo
											.getGs_concentricity_id().trim());
									bean.setProviderCode(providerCode);
									bean.setProductType(productType.equals("") ? bean.getProductType()
											: Integer.parseInt(productType));
									bean.setIsBid(productBid.equals("") ? bean.getIsBid()
											: Integer.parseInt(productBid));
									bean.setLastUpdateTime(new DateUtil()
											.getFullString());
									bean.setLastUpdateUser(context
											.getLoginUserName());
									bean.setLastUpdateUserId(context
											.getLoginUserId());
									dao.update(bean);
								} else {
									bean = new GSProductTypeBean();
									bean.setCreateTime(new DateUtil()
											.getFullString());
									bean.setCreateUser(context
											.getLoginUserName());
									bean.setCreateUserId(context
											.getLoginUserId());
									bean.setIsBalance(1);
									bean.setIsBid(productBid.equals("") ? GSBaseConstants.PRODUCT_TYPE_MANAGER_ORTHER_BID
											: Integer.parseInt(productBid));
									bean.setLastUpdateTime(new DateUtil()
											.getFullString());
									bean.setLastUpdateUser(context
											.getLoginUserName());
									bean.setLastUpdateUserId(context
											.getLoginUserId());
									bean.setProductCode(vo
											.getGs_concentricity_id().trim());
									bean.setProductType(productType.equals("") ? GSBaseConstants.PRODUCT_TYPE_MANAGER_ORTHER_USE
											: Integer.parseInt(productType));
									bean.setProviderCode(providerCode);
									bean.setProductId(mbean.getId());
									bean.setRemark("");
									bean.setStatus(Integer
											.parseInt(ConstDefine.STR_VALID_YES));
									dao.insert(bean);
								}

									GSProductBidBean bidBean = bidDao
											.findProductBidByProuductCodeNoStauts(vo
													.getGs_concentricity_id()
													.trim());
									if (bidBean != null) {
										bidBean.setStatus(1);
										bidBean.setProductName(productName);
										bidBean.setProductId(mbean.getId());
										bidBean.setProductCode(vo
												.getGs_concentricity_id()
												.trim());
										bidBean.setProviderCode(providerCode);
										bidBean.setProductType(productBid
												.equals("") ? bidBean
												.getProductType() : Integer
												.parseInt(productBid));
										bidBean.setLastUpdateTime(new DateUtil()
												.getFullString());
										bidBean.setLastUpdateUser(context
												.getLoginUserName());
										bidBean.setLastUpdateUserId(context
												.getLoginUserId());
										bidDao.update(bidBean);
									} else {
										bidBean = new GSProductBidBean();
										bidBean.setCreateTime(new DateUtil()
												.getFullString());
										bidBean.setCreateUser(context
												.getLoginUserName());
										bidBean.setCreateUserId(context
												.getLoginUserId());
										bidBean.setProductName(productName);
										bidBean.setLastUpdateTime(new DateUtil()
												.getFullString());
										bidBean.setLastUpdateUser(context
												.getLoginUserName());
										bidBean.setLastUpdateUserId(context
												.getLoginUserId());
										bidBean.setProductCode(vo
												.getGs_concentricity_id()
												.trim());
										bidBean.setProductType(productBid
												.equals("") ? GSBaseConstants.PRODUCT_TYPE_MANAGER_ORTHER_BID
												: Integer.parseInt(productBid));
										bidBean.setProviderCode(providerCode);
										bidBean.setProductId(mbean.getId());
										bidBean.setRemark("");
										bidBean.setStatus(Integer
												.parseInt(ConstDefine.STR_VALID_YES));
										bidDao.insert(bidBean);
									}
						}
					} else {
						if (GSBaseConstants.IS_CENTER_WAREHOUSE!=null&&GSBaseConstants.IS_CENTER_WAREHOUSE.equals("true")) {
							bl = processor.saveManufactureKM(vo,providerid,planprice);
						}else{
							bl = processor.saveManufactureKM(vo);
						}
					
						GSManufactureBean productBean = processor
								.getManufInfoByConcentricityId(vo
										.getGs_concentricity_id());
						if (productBean != null) {
							productStuationAnalyse.insert(
									processor.appendInsertContent(vo),
									vo.getCreate_time(),
									context.getLoginUserId(),
									context.getLoginUserName(),
									productBean.getId());
						}
						GSProductTypeBean bean = dao
								.findProductTypeByProuductCodeNoStauts(vo
										.getGs_concentricity_id().trim());
						if (bean != null) {
							bean.setStatus(Integer
									.parseInt(ConstDefine.STR_VALID_YES));
							bean.setProductCode(vo.getGs_concentricity_id()
									.trim());
							bean.setProviderCode(vo.getGs_provider_id());
							bean.setProductType(productType.equals("") ? GSBaseConstants.PRODUCT_TYPE_MANAGER_ORTHER_USE
									: Integer.parseInt(productType));
	
								bean.setIsBid(productBid.equals("") ? GSBaseConstants.PRODUCT_TYPE_MANAGER_ORTHER_BID
										: Integer.parseInt(productBid));
							bean.setLastUpdateTime(new DateUtil()
									.getFullString());
							bean.setLastUpdateUser(context.getLoginUserName());
							bean.setLastUpdateUserId(context.getLoginUserId());
							dao.update(bean);
						} else {
							bean = new GSProductTypeBean();
							bean.setCreateTime(new DateUtil().getFullString());
							bean.setCreateUser(context.getLoginUserName());
							bean.setCreateUserId(context.getLoginUserId());
							bean.setIsBalance(1);
							
								bean.setIsBid(productBid.equals("") ? GSBaseConstants.PRODUCT_TYPE_MANAGER_ORTHER_BID
										: Integer.parseInt(productBid));
	
							bean.setLastUpdateTime(new DateUtil()
									.getFullString());
							bean.setLastUpdateUser(context.getLoginUserName());
							bean.setLastUpdateUserId(context.getLoginUserId());
							bean.setProductCode(vo.getGs_concentricity_id()
									.trim());
							bean.setProductType(productType.equals("") ? GSBaseConstants.PRODUCT_TYPE_MANAGER_ORTHER_USE
									: Integer.parseInt(productType));
							bean.setProviderCode(vo.getGs_provider_id());
							bean.setProductId(productBean != null ? productBean
									.getId() : "");
							bean.setRemark("");
							bean.setStatus(Integer
									.parseInt(ConstDefine.STR_VALID_YES));
							dao.insert(bean);
						}
//						if (productType.equals(GSBaseConstants.PRODUCT_TYPE1)) {
							GSProductBidBean bidBean = bidDao
									.findProductBidByProuductCodeNoStauts(vo
											.getGs_concentricity_id().trim());
							if (bidBean != null) {
								bidBean.setProductId(productBean != null ? productBean
										.getId() : "");
								bidBean.setProductCode(vo
										.getGs_concentricity_id().trim());
								bidBean.setProviderCode(vo.getGs_provider_id());
								bidBean.setProductType(productBid.equals("") ? GSBaseConstants.PRODUCT_TYPE_MANAGER_ORTHER_BID
										: Integer.parseInt(productBid));
								bidBean.setLastUpdateTime(new DateUtil()
										.getFullString());
								bidBean.setLastUpdateUser(context
										.getLoginUserName());
								bidBean.setLastUpdateUserId(context
										.getLoginUserId());
								bidBean.setStatus(Integer
										.parseInt(ConstDefine.STR_VALID_YES));
								bidDao.update(bidBean);
							} else {
								bidBean = new GSProductBidBean();
								bidBean.setCreateTime(new DateUtil()
										.getFullString());
								bidBean.setCreateUser(context
										.getLoginUserName());
								bidBean.setCreateUserId(context
										.getLoginUserId());
								bidBean.setProductName(vo
										.getGs_manufacture_name());
								bidBean.setLastUpdateTime(new DateUtil()
										.getFullString());
								bidBean.setLastUpdateUser(context
										.getLoginUserName());
								bidBean.setLastUpdateUserId(context
										.getLoginUserId());
								bidBean.setProductCode(vo
										.getGs_concentricity_id().trim());
								bidBean.setProductType(productBid.equals("") ? GSBaseConstants.PRODUCT_TYPE_MANAGER_ORTHER_BID
										: Integer.parseInt(productBid));
								bidBean.setProviderCode(vo.getGs_provider_id()
										.trim());
								bidBean.setProductId(productBean != null ? productBean
										.getId() : vo.getId());
								bidBean.setRemark("");
								bidBean.setStatus(Integer
										.parseInt(ConstDefine.STR_VALID_YES));
								bidDao.insert(bidBean);
							}
//						}
					}
					if (bl) {
						// 移除
						int row = array.size();
						for (int j = 0; j < row; j++) {
							GSManufactureView mv = (GSManufactureView) array
									.get(j);
							if (index[a].equals(mv.getGs_recorder_id())) {
								array.remove(mv);
								break;
							}
						}
					}
				}

				if (array != null && array.size() == 0) {
					aForm.getSession().removeAttribute("manufactureExcel");
					aForm.getRequest().setAttribute("message",
							"产品确认写入成功，无可确认产品信息，返回继续添加。");
					if (flag) {
						aForm.getRequest().setAttribute("action", "A400412.do");
					} else
						aForm.getRequest().setAttribute("action", "A400313.do");
				} else if (array != null && array.size() > 0) {
					aForm.getRequest().setAttribute("message", "产品确认写入成功");
					if (aForm.getRequest().getParameter("choice").equals("")) {
						if (flag) {
							aForm.getRequest().setAttribute("action",
									"A400327.do");
						} else
							aForm.getRequest().setAttribute("action",
									"A400312.do");
					}
				}

			}
		} catch (GSException e) {
			GSLogWriter.error(this, e.toString());
			if (flag) {
				aForm.getRequest().setAttribute("action", "A400327.do");
			} else
				aForm.getRequest().setAttribute("action", "A400312.do");
			aForm.getRequest().setAttribute("target", "_self");
			throw new GSException(e.getMessage());
		} finally {
			rst.setResult(array);
			aForm.getSession().setAttribute("manufactureExcel", rst);
		}
		return GSBaseConstants.FORWARD_SUCCESS;
	}
}
